package com.example.tonjie.software_park_association;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Personal_data extends AppCompatActivity {

    public static final int TAKE_PHOTO = 1;
    private RelativeLayout relativeLayout_Head_portrait;//为图片的相对布局添加监听事件
    private ImageView imageView_Avatar_images,im1;//要更换的图片
    private Uri imageUri;//

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_data);
        real();
        imageView_Avatar_images = (ImageView) findViewById(R.id.Avatar_images);
        im1= (ImageView) findViewById(R.id.im1);
    }


    /**
     * 为图片的相对布局实现点击事件
     */
    private void real() {
        relativeLayout_Head_portrait = (RelativeLayout) findViewById(R.id.Head_portrait);
        relativeLayout_Head_portrait.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
//                File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
//                try {
//                    if (outputImage.exists()) {
//                        outputImage.delete();
//                    }
//                    outputImage.createNewFile();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                if (Build.VERSION.SDK_INT >= 24) {
//                    imageUri = FileProvider.getUriForFile(Personal_data.this, "com.exaple.cameraalbumtest.fileprivider", outputImage);
//                } else {
//                    imageUri = Uri.fromFile(outputImage);
//                }
//                启动相机程序
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, TAKE_PHOTO);
                Toast.makeText(Personal_data.this, "Hello", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        switch (requestCode) {
//            case TAKE_PHOTO:
//                if(requestCode==RESULT_OK){
//                    try {
//                        Bitmap bitmpa = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
//                        imageView_Avatar_images.setImageBitmap(bitmpa);
//                    } catch (FileNotFoundException e) {
//                        e.printStackTrace();
//                    }
//                }
//                break;
        if (resultCode == RESULT_OK) {
            if (requestCode == TAKE_PHOTO) {
                Bundle bumdle=data.getExtras();
                Bitmap bitmap= (Bitmap) bumdle.get("data");
                im1.setImageBitmap(bitmap);
            }
        }
    }

}
